#include "StdAfx.h"
#include "SeverData.h"
#include "shlwapi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

CSeverData::CSeverData(void)
{
	InitializeCriticalSection(&m_CriticalSection);
	Initialize();
}
CSeverData::~CSeverData()
{
	DeleteCriticalSection(&m_CriticalSection);
}

CSeverData::CSeverData(CDataManager *pData)
{
	InitializeCriticalSection(&m_CriticalSection);
	m_pData = pData;
	Initialize();
}

void CSeverData::Initialize()
{
	Lock();
	m_pData->getAreaInfo(m_vAreaInfo);
	m_pData->getAccountInfo(m_vUserInfo);

	UnLock();
}

void CSeverData::addNewDesktop(const desktopInfo& desktop,int areaIndex)
{
	Lock();
	areaInfo& area = m_vAreaInfo[areaIndex];
	area.desktops.push_back(desktop);
	m_pData->addDesktop(desktop,(wchar_t*)area.areaName);
	UnLock();
}

void CSeverData::delNewDesktop(const wchar_t* desktop,int areaIndex)
{
	Lock();
	areaInfo& area = m_vAreaInfo[areaIndex];
	m_pData->removeDesktop(desktop,(wchar_t*)area.areaName);
	m_pData->getAreaInfo(m_vAreaInfo);
	UnLock();

}

void CSeverData::delArea(const wchar_t* areaName)
{
	Lock();
	m_pData->delArea(areaName);
	m_pData->getAreaInfo(m_vAreaInfo);
	UnLock();
}

void CSeverData::addArea(const wchar_t* areaName)
{
	Lock();
	m_pData->addArea(areaName);
	m_pData->getAreaInfo(m_vAreaInfo);
	UnLock();
}

bool CSeverData::checkPasswdIsRight(const userAccount& user,int& right)
{
	Lock();
	for (size_t i = 0; i < m_vUserInfo.size(); i++)
	{
		if(StrCmp(user.username,m_vUserInfo[i].username) == 0)
		{
			if (StrCmp(user.passwd,m_vUserInfo[i].passwd) == 0)
			{
				right = m_vUserInfo[i].right;
				return true;
			}
			else
				return false;
		}
	}
	return false;
}

void CSeverData::getEmployeeShort(employeeShortInfo& shortInfo,const CString& strId)
{
	Lock();
	m_pData->getEmployeeShort(shortInfo,strId);
	UnLock();
}

void CSeverData::setBookStateReady(const CTime& startTime,const CTime& endTime)
{
	Lock();
	m_pData->SetBookStateReady(startTime,endTime);
	m_pData->getAreaInfo(m_vAreaInfo);
	UnLock();
}

void CSeverData::setValidBookState(const CTime& endTime)
{
	Lock();
	m_pData->SetInvidBookState(endTime);
	m_pData->getAreaInfo(m_vAreaInfo);
	UnLock();
}
void CSeverData::GetBookData(vector<bookManagerData> &pData, const CTime &nowTime, const wchar_t *deskName, const wchar_t *areaName)
{
	Lock();
	m_pData->GetPreserveData(pData,nowTime,deskName,areaName);
	UnLock();
}

void CSeverData::addBookData(const newBookData& pData)
{
	Lock();
	m_pData->addPreserveData(pData);
	UnLock();
}

void CSeverData::delBookData(const hjcTime& setTime,const hjcDate& setDate)
{
	Lock();
	m_pData->delPreserveData(CTime(setDate.year,setDate.month,setDate.day,setTime.hour,setTime.minute,setTime.second));
	UnLock();
}

void CSeverData::addOrder(const INT64 &serial, const Order &vOrder)
{
	Lock();
	m_pData->addOrder(vOrder,serial);
	m_pData->setDesktopState(vOrder.areaName,vOrder.deskName,DESKSTATE_RED_ORDER);
	CString areaName(vOrder.areaName);
	CString deskName(vOrder.deskName);
	deskMap& dMap = m_areaMap[areaName];
	dMap[deskName] = serial;
	UnLock();
}

void CSeverData::updateOrder(const INT64& serial,const Order& order)
{
	Lock();
	m_pData->delOrder(serial);
	m_pData->addOrder(order,serial);
	m_pData->setDesktopState(order.areaName,order.deskName,DESKSTATE_RED_ORDER);
	m_pData->getAreaInfo(m_vAreaInfo);

	CString areaName(order.areaName);
	CString deskName(order.deskName);
	deskMap& dMap = m_areaMap[areaName];
	dMap[deskName] = serial;
	//m_pData->updateOrder(serial,order);
	UnLock();
}

void CSeverData::getOrder(const INT64& serial, Order& order)
{
	Lock();
	m_pData->getOrder(serial,order);
	UnLock();
}

foodParam CSeverData::getFoodParamByID(const wchar_t *id)
{
	return m_pData->getFoodParam(id,true);
}

foodParam CSeverData::getFoodParamByAbbr(const wchar_t* abbr)
{
	return m_pData->getFoodParam(abbr,false);
}

INT64 CSeverData::getSerial(const wchar_t* areaName,const wchar_t* deskName)
{
	CString CAreaName(areaName);
	CString CDeskName(deskName);
	deskMap dMap = m_areaMap[CAreaName];
	return  dMap[CDeskName];
}

void CSeverData::getDiscount(vector<discount>& vDiscount,const wchar_t* discountName)
{
	Lock();
	m_pData->getDiscount(vDiscount,discountName);
	UnLock();
}

void CSeverData::checkOut(const wchar_t* areaName,const wchar_t* deskName)
{
	Lock();
	m_pData->setDesktopState(areaName,deskName,DESKSTATE_RED_CASH);
	UnLock();
}

void CSeverData::setState(const setDeskState* state)
{
	Lock();
	m_pData->setDesktopState(state->areaName,state->deskName,state->nState);
	UnLock();
}